package gupaoedu.LazySingleton;

import gupaoedu.enumSingleton.EnumSingleton;
import javafx.beans.binding.ObjectExpression;

import java.io.*;

/**
 * gupaoedu.LazySingleton
 * linwenjie
 * 2019-06-19
 * 序列化测试 破坏单例
 */
public class SerializeSingletonTest {
    public static void main(String[] args) {
        /*LazySingleton lazySingleton = LazySingleton.getInstance();


        try {
            FileOutputStream fos = new FileOutputStream("LazySingLetion.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(lazySingleton);
            oos.flush();
            oos.close();

            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("LazySingLetion.obj"));

            LazySingleton lazySingleton2 = (LazySingleton) ois.readObject();

            ois.close();
            System.out.println(lazySingleton);
            System.out.println(lazySingleton2);
            System.out.println(lazySingleton == lazySingleton2);
        } catch (Exception e) {
            e.printStackTrace();
        }*/

        //----------------测试 枚举单例 ------------------//
        EnumSingleton singleton = EnumSingleton.getInstance();


        try {
            Object obj = new Object();
            singleton.setData(obj);

            FileOutputStream fos = new FileOutputStream("EnumSingleton.obj");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(singleton);
            oos.flush();
            oos.close();

            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("EnumSingleton.obj"));

            EnumSingleton singleton2 = (EnumSingleton) ois.readObject();

            ois.close();
            System.out.println(singleton.getData());
            System.out.println(singleton2.getData());
            System.out.println(singleton.getData() == singleton2.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
